
angular.module('mamApp').controller('loginController', ['$scope', 'AuthManager', '$state', 'api', function ($scope, AuthManager, $state, api) {
    $scope.user = {};
    $scope.err = {};
  

    if (localStorage.getItem("userInfo")) {
        var tmpUserInfo = JSON.parse(localStorage.getItem("userInfo"));
        $scope.user = tmpUserInfo.remember ? tmpUserInfo : {};
    }
    function randomString(len) {
        len = len || 32;
        var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
        var maxPos = $chars.length;
        var pwd = '';
        for (var i = 0; i < len; i++) {
            pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return pwd;
    }

    var randomCode = "";

    $scope.codeRefrsh = function () {
        randomCode = randomString(12);
        $scope.code = api + "/code/kaptcha?key=" + randomCode;
    }
   $scope.codeRefrsh();
  /*  setInterval(function () {
        if ($scope.bgSelect == 2) {
            $scope.$apply($scope.bgSelect = 0)
        } else {
            $scope.$apply($scope.bgSelect = $scope.bgSelect + 1);
        }
    }, 5000)*/




    $scope.login = function () {
        if ($scope.user.userName && $scope.user.password && $scope.user.validateCode) {
            AuthManager.login({
                userName: $scope.user.userName,
                password: $scope.user.password,
                validateCode: $scope.user.validateCode
            }, randomCode).then(function (result) {
                if (result.data.c == "s") {
                    if ($scope.user.remember) {
                        result.data.userAdminVO.remember = $scope.user.remember;
                        result.data.userAdminVO.password = $scope.user.password;
                    }
                    localStorage.setItem("userInfo", JSON.stringify(result.data.userAdminVO));
                    $state.go("main.home",{slideIsOpen:true})
                } else {
                    $scope.err.page = result.data.i;
                    $scope.codeRefrsh();
                }
            })
        } else if(!$scope.user.userName) {
            $scope.err.page = "用户名不能为空";
        } else if (!$scope.user.password){
            $scope.err.page = "密码不能为空"

        }else {
            $scope.err.page = "验证码不能为空"
        }

    }

    var ratioData = 0.49479;//图片比例 950 / 1920
    $(document).ready(function () {
        var $startBg = $("#imgBg"),
            $startBgImg = $("#imgBg img.rImg"),
            pageWidth = $("html").innerWidth(),
            pageHeight = $("html").innerHeight();

        setPageSize(pageWidth, pageHeight);
        var timer = null;
        clearTimeout(timer);
        var _self = $(this);
        pageWidth = _self.innerWidth(),
            pageHeight = _self.innerHeight();
        setPageSize(pageWidth, pageHeight);
        $(window).resize(function () {
            clearTimeout(timer);
            var _self = $(this);
            timer = setTimeout(function () {
                var pageWidth = _self.innerWidth(),
                    pageHeight = _self.innerHeight();
                setPageSize(pageWidth, pageHeight);
            }, 100);
        });

        function setPageSize(Width, Height) {
            $startBg.css({
                width: Width,
                height: Height
            });
            var pageRatio = ((Height / Width) * 10000) / 10000;

            if (pageRatio < ratioData) {
                var ratio_height = Width * ratioData,
                    location_top = (ratio_height - Height) / 2;

                $startBgImg.css({
                    width: Width,
                    height: ratio_height,
                    top: "-" + location_top + "px",
                    left: 0
                });
            } else if (pageRatio > ratioData) {
                var ratio_width = Height / ratioData,
                    location_left = (ratio_width - Width) / 2;
                $startBgImg.css({
                    width: ratio_width,
                    height: Height,
                    left: "-" + location_left + "px",
                    top: 0
                });
            } else {
                $startBgImg.css({
                    width: Width,
                    height: pageHeight,
                    top: 0,
                    left: 0
                });
            }
        }
    });

    $scope.keyup=function (e) {

        if(e.key=="Enter"){
            $scope.login();
        }
    }



}])

